﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace KLTN
{
    public class SpringPendulum
    {
        public float Amplitude { get; set; } //Biên độ dao động
        public double AngleFre { get; set; } //Tần số góc
        public double AngleBegin { get; set; } //Pha ban đầu
        public CModel Ball { get; set; } //Model
        public Vector3 Position { get; set; }//Vị trí ban đầu

        private float _time;
        private CCamera camera;
        public SpringPendulum()
        { 
            
        }
        public SpringPendulum(CModel SPModel, float Amplitude,double AngleFre,double AngleBegine, Vector3 Position)
        {
            this.Ball = SPModel;
            this.Amplitude = Amplitude;
            this.AngleFre = AngleFre;
            this.AngleBegin = AngleBegin;
            this.Position = Position;
            //
            
        }
        /// <summary>
        /// load Content (Ham 1)
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="graphicDevice"></param>
        public void LoadContent(ContentManager Content, GraphicsDevice graphicDevice)
        {
            camera = new FreeCamera(new Vector3(0, 0, -2000), MathHelper.ToRadians(153), MathHelper.ToRadians(5), graphicDevice);
        }
        /// <summary>
        /// Uodate (Ham 2)
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            Move(gameTime);
            UpdateCamera();
        }
        /// <summary>
        /// Draw (Ham 3)
        /// </summary>
        public void Draw()
        {
            if (camera.BoudingVolumeslnView(Ball.BoudingSphere))
            {
                Ball.Draw(camera.View, camera.Projection);
            }
        }
        public void Move(GameTime gameTime)
        {
            
            _time+=(float)gameTime.ElapsedGameTime.Milliseconds/1000;
            Vector3 EquationMove= new Vector3(0,(float)(Amplitude*Math.Cos(AngleFre*_time+AngleBegin)),0);//x=Acos(wt+a)
            Ball.Positon = EquationMove+Position;//vi tri ban dau
        }
        public void UpdateCamera()
        {
            KeyboardState keystate = Keyboard.GetState();
            if (keystate.IsKeyDown(Keys.Left))
                ((FreeCamera)camera).Move(Vector3.Left*10);
            if (keystate.IsKeyDown(Keys.Right))
                ((FreeCamera)camera).Move(Vector3.Right*10);
            if (keystate.IsKeyDown(Keys.Up))
                ((FreeCamera)camera).Move(Vector3.Up*10);
            if (keystate.IsKeyDown(Keys.Down))
                ((FreeCamera)camera).Move(Vector3.Down*10);
            camera.Update();
        }
       
    }
}
